import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.swtdesigner.SWTResourceManager;

public class Main {

	private static final String SUGGEST_WITDH_FONT11 = "Suggest font 11";
	private static final String SUGGEST_WITDH_FONT10 = "Suggest font 10";
	private static final String FILE_ITEM_RULES = "Unit Test checklist.xls";
	private static final String FILE_PATH = "FilePath.txt";

	protected Display display;
	protected Shell shell;
	private Text txtFilename;
	private Combo comboKeyFilter;
	private Label lblOpenFile;
	private Label lblKeyFilter;
	private FormData formData_1;
	private Button btnOpenFile;
	private TabFolder tabFolder;
	private String filename;
	private Button chkHideBlankColumn;

	HashMap mapTable = new HashMap(); // <String, Table>
	HashMap mapQueue = new HashMap(); // <String, String[width, data,
	// alignment][]>
	List listNode = new LinkedList();
	List listItem = new LinkedList();
	List itemRules = new LinkedList();
	TreeSet idItem = new TreeSet();
	private Button chkCellWidth;

	String[] CheckListByNode = { "Check Version", "Check for Readonly Field",
			"Check for Hidden Field", "Check Maxlength", "Check Position",
			"Check ID", "Check Item by Item Rules" };
	String[] CheckList = { "Check Related Position", "Check Related ID" };
	List menuItemListByNode = new LinkedList();
	List menuItemList = new LinkedList();
	private FormData formData_2;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage("Vietnam-Flag-icon.png"));
		shell.setLayout(new GridLayout(1, false));
		shell.setSize(800, 600);
		shell.setText("SDL Validator");
		{
			Menu menu = new Menu(shell, SWT.BAR);
			shell.setMenuBar(menu);
			{
				MenuItem menuItem = new MenuItem(menu, SWT.NONE);
				menuItem.addSelectionListener(new SelectionAdapter() {

					public void widgetSelected(SelectionEvent e) {
						validate();
					}
				});
				menuItem.setText("Validate");
			}
			{
				MenuItem menuItem2 = new MenuItem(menu, SWT.NONE);
				menuItem2.addSelectionListener(new SelectionAdapter() {

					public void widgetSelected(SelectionEvent e) {
						saveCSVFile();
					}
				});
				menuItem2.setText("Save to CSV file");
			}
			{
				MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
				menuItem.setText("Option Check");
				Menu checkMenu = new Menu(menu);
				menuItem.setMenu(checkMenu);
				for (int i = 0; i < CheckListByNode.length; i++) {
					final MenuItem newItem = new MenuItem(checkMenu, SWT.NONE
							| SWT.CHECK);
					newItem.setText(CheckListByNode[i]);
					newItem.setSelection(true);
					newItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent arg0) {
							System.out.print(newItem.getSelection());
							newItem.setSelection(newItem.getSelection());
						}
					});
					menuItemListByNode.add(newItem);
				}
				new MenuItem(checkMenu, SWT.SEPARATOR);
				for (int i = 0; i < CheckList.length; i++) {
					final MenuItem newItem = new MenuItem(checkMenu, SWT.NONE
							| SWT.CHECK);
					newItem.setText(CheckList[i]);
					newItem.setSelection(true);
					newItem.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent arg0) {
							System.out.print(newItem.getSelection());
							newItem.setSelection(newItem.getSelection());
						}
					});
					menuItemList.add(newItem);
				}
				{
					MenuItem menuItem1 = new MenuItem(menu, SWT.NONE);
					menuItem1.addSelectionListener(new SelectionAdapter() {

						public void widgetSelected(SelectionEvent e) {
							// try {
							// File file = new File("SDLHelp.CHM");
							// Desktop desktop = Desktop.getDesktop();
							// desktop.open(file);
							// } catch (Exception ex) {
							//
							// }
							try {
								Runtime
										.getRuntime()
										.exec(
												"rundll32 url.dll,FileProtocolHandler SDLHelp.CHM");
							} catch (Exception ex) {
								ex.printStackTrace();
								System.out.println(ex.getMessage());
							}
						}
					});
					menuItem1.setText("Help");
				}
			}

		}
		{
			Group grpOpenFiles = new Group(shell, SWT.NONE);
			{
				GridData gridData = new GridData(SWT.FILL, SWT.TOP, true,
						false, 1, 1);
				gridData.heightHint = 78;
				gridData.widthHint = 767;
				grpOpenFiles.setLayoutData(gridData);
			}
			grpOpenFiles.setLayout(new FormLayout());
			{
				lblOpenFile = new Label(grpOpenFiles, SWT.NONE);
				lblOpenFile.setSize(lblOpenFile.getSize().x, 200);
				lblOpenFile.setFont(new Font(null, "Segoe UI", 12, SWT.NONE));
				{
					FormData formData = new FormData();
					formData.width = 72;
					formData.top = new FormAttachment(0);
					formData.left = new FormAttachment(0, 20);
					lblOpenFile.setAlignment(SWT.RIGHT);
					lblOpenFile.setLayoutData(formData);
				}
				lblOpenFile.setText("Open file:");
			}

			txtFilename = new Text(grpOpenFiles, SWT.BORDER);
			txtFilename.setSize(txtFilename.getSize().x, 200);
			{
				formData_1 = new FormData();
				formData_1.bottom = new FormAttachment(lblOpenFile, 0,
						SWT.BOTTOM);
				formData_1.left = new FormAttachment(lblOpenFile, 6);
				formData_1.top = new FormAttachment(lblOpenFile, 0, SWT.TOP);
				formData_1.width = 462;
				txtFilename.setLayoutData(formData_1);
			}

			{
				lblKeyFilter = new Label(grpOpenFiles, SWT.NONE);
				lblKeyFilter.setSize(lblKeyFilter.getSize().x, 200);
				lblKeyFilter.setFont(new Font(null, "Segoe UI", 12, SWT.NONE));
				{
					FormData formData = new FormData();
					formData.width = 72;
					formData.top = new FormAttachment(60);
					formData.left = new FormAttachment(0, 20);
					lblKeyFilter.setAlignment(SWT.RIGHT);
					lblKeyFilter.setLayoutData(formData);
				}
				lblKeyFilter.setText("Key filter:");
			}

			comboKeyFilter = new Combo(grpOpenFiles, SWT.BORDER);
			comboKeyFilter.setSize(comboKeyFilter.getSize().x, 200);
			{
				formData_1 = new FormData();
				formData_1.bottom = new FormAttachment(lblKeyFilter, 0,
						SWT.BOTTOM);
				formData_1.left = new FormAttachment(lblKeyFilter, 6);
				formData_1.top = new FormAttachment(lblKeyFilter, 0, SWT.TOP);
				formData_1.width = 300;
				comboKeyFilter.setLayoutData(formData_1);
				comboKeyFilter.setVisibleItemCount(10);
			}

			{
				btnOpenFile = new Button(grpOpenFiles, SWT.NONE);
				btnOpenFile.addMouseListener(new MouseAdapter() {

					public void mouseDown(MouseEvent e) {
						openFile();
					}
				});
				btnOpenFile.addSelectionListener(new SelectionAdapter() {

					public void widgetSelected(SelectionEvent e) {
					}
				});
				{
					formData_2 = new FormData();
					formData_2.left = new FormAttachment(txtFilename, 6);
					formData_2.top = new FormAttachment(lblOpenFile, 0, SWT.TOP);
					formData_2.width = 60;
					formData_2.height = 22;
					btnOpenFile.setLayoutData(formData_2);
				}
				btnOpenFile.setText("Browse...");
			}
			{
			}
			{
				chkHideBlankColumn = new Button(grpOpenFiles, SWT.CHECK);
				chkHideBlankColumn.setSelection(true);
				chkHideBlankColumn.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						hideBlankData(chkHideBlankColumn.getSelection());
					}
				});
				{
					FormData formData = new FormData();
					formData.left = new FormAttachment(txtFilename, 0, SWT.LEFT);
					formData.top = new FormAttachment(txtFilename, 4);
					chkHideBlankColumn.setLayoutData(formData);
				}
				chkHideBlankColumn.setText("Hide Blank Column");
			}
			{
				chkCellWidth = new Button(grpOpenFiles, SWT.CHECK);
				{
					FormData formData = new FormData();
					formData.bottom = new FormAttachment(chkHideBlankColumn, 0,
							SWT.BOTTOM);
					formData.left = new FormAttachment(chkHideBlankColumn, 12);
					chkCellWidth.setLayoutData(formData);
				}
				chkCellWidth.setText("Show Input Cell");
				chkCellWidth.setSelection(false);
				chkCellWidth.setEnabled(false);
				chkCellWidth.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						hideCellWidthValidator(!chkCellWidth.getSelection());
					}
				});
			}

			{
				tabFolder = new TabFolder(shell, SWT.NONE);
				{
					GridData gridData = new GridData(SWT.FILL, SWT.FILL, true,
							true, 1, 1);
					gridData.heightHint = 402;
					gridData.widthHint = 570;
					tabFolder.setLayoutData(gridData);
				}
			}
		}
	}

	private void createTabItem() {
		for (int i = tabFolder.getItemCount() - 1; i >= 0; i--) {
			tabFolder.getItem(i).dispose();
		}
		createLogTab();
		itemRules = ExcelReader.ReadItemRules(FILE_ITEM_RULES);
		TreeSet set = new TreeSet();
		for (int i = 0; i < ComponentClass.components.length; i++) {
			set.add(ComponentClass.components[i]);
		}
		Object[] items = set.toArray();
		for (int i = 0; i < items.length; i++) {
			TabItem item = new TabItem(tabFolder, SWT.NONE, i + 1);
			item.setText(items[i].toString());
			final Table table = new Table(tabFolder, SWT.FULL_SELECTION
					| SWT.VIRTUAL);
			table.setBackground(Const.DEFAULT_COLOR);
			mapTable.put(items[i].toString(), table);
			table.setHeaderVisible(true);
			table.setLinesVisible(true);
			table.addListener(SWT.DefaultSelection, new Listener() {
				public void handleEvent(Event arg0) {
					table.forceFocus();
					table.deselectAll();
				}
			});
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText("No.");
			column.setWidth(40);
			column.setAlignment(SWT.CENTER);
			if (item.getText().equals(ComponentClass.button)) {
				for (int j = 0; j < PropertyClass.propertiesForButton.length; j++) {
					column = new TableColumn(table, SWT.NONE);
					column.setText(PropertyClass.propertiesForButton[j]);
					column.setWidth(PropertyClass.proColForButton[j][0]);
					column.setToolTipText(String
							.valueOf(PropertyClass.proColForButton[j][0]));
					column.setAlignment(PropertyClass.proColForButton[j][1]);
				}
				item.setControl(table);
			}
			if (item.getText().equals(ComponentClass.combo)) {
				for (int j = 0; j < PropertyClass.propertiesForCombo.length; j++) {
					column = new TableColumn(table, SWT.NONE);
					column.setText(PropertyClass.propertiesForCombo[j]);
					column.setWidth(PropertyClass.proColForCombo[j][0]);
					column.setToolTipText(String
							.valueOf(PropertyClass.proColForCombo[j][0]));
					column.setAlignment(PropertyClass.proColForCombo[j][1]);
				}
				item.setControl(table);
			}
			if (item.getText().equals(ComponentClass.group)) {
				for (int j = 0; j < PropertyClass.propertiesForGroup.length; j++) {
					column = new TableColumn(table, SWT.NONE);
					column.setText(PropertyClass.propertiesForGroup[j]);
					column.setWidth(PropertyClass.proColForGroup[j][0]);
					column.setToolTipText(String
							.valueOf(PropertyClass.proColForGroup[j][0]));
					column.setAlignment(PropertyClass.proColForGroup[j][1]);

				}
				item.setControl(table);
			}
			if (item.getText().equals(ComponentClass.text)) {
				for (int j = 0; j < PropertyClass.propertiesForText.length; j++) {
					column = new TableColumn(table, SWT.NONE);
					column.setText(PropertyClass.propertiesForText[j]);
					column.setWidth(PropertyClass.proColForText[j][0]);
					column.setToolTipText(String
							.valueOf(PropertyClass.proColForText[j][0]));
					column.setAlignment(PropertyClass.proColForText[j][1]);
					if (PropertyClass.propertiesForText[j]
							.equals(PropertyClass.width)) {
						column = new TableColumn(table, SWT.NONE);
						column.setText(Const.SHOW_LENGTH);
						column.setWidth(Const.DEFAULT_WIDTH * 2);
						column.setToolTipText(String
								.valueOf(Const.DEFAULT_WIDTH * 2));
						EditorCreator.createTextEditor(table, itemRules,
								Const.SHOW_LENGTH);
						column = new TableColumn(table, SWT.NONE);
						column.setText(SUGGEST_WITDH_FONT11);
						column.setWidth(80);
						column.setToolTipText(String.valueOf(80));
						column = new TableColumn(table, SWT.NONE);
						column.setText(SUGGEST_WITDH_FONT10);
						column.setWidth(80);
						column.setToolTipText(String.valueOf(80));
					}
				}
				column = new TableColumn(table, SWT.NONE);
				column.setText(Const.ITEM_RULES);
				column.setWidth(Const.DEFAULT_WIDTH * 2);
				column.setToolTipText(String.valueOf(Const.DEFAULT_WIDTH * 2));
				EditorCreator.createItemRulesComboCell(table, itemRules);
				column = new TableColumn(table, SWT.NONE);
				column.setText(Const.ITEM_RULES_LABEL);
				column.setWidth(Const.DEFAULT_WIDTH * 2);
				column.setToolTipText(String.valueOf(Const.DEFAULT_WIDTH * 2));
				EditorCreator.createTextEditor(table, itemRules,
						Const.ITEM_RULES_LABEL);
				item.setControl(table);
			}
			if (item.getText().equals(ComponentClass.label)) {
				for (int j = 0; j < PropertyClass.propertiesForLabel.length; j++) {
					column = new TableColumn(table, SWT.NONE);
					column.setText(PropertyClass.propertiesForLabel[j]);
					column.setWidth(PropertyClass.proColForLabel[j][0]);
					column.setToolTipText(String
							.valueOf(PropertyClass.proColForLabel[j][0]));
					column.setAlignment(PropertyClass.proColForLabel[j][1]);
					if (PropertyClass.propertiesForText[j]
							.equals(PropertyClass.width)) {
						column = new TableColumn(table, SWT.NONE);
						column.setText(Const.SHOW_LENGTH);
						column.setWidth(Const.DEFAULT_WIDTH * 2);
						column.setToolTipText(String
								.valueOf(Const.DEFAULT_WIDTH * 2));
						EditorCreator.createTextEditor(table, itemRules,
								Const.SHOW_LENGTH);
						column = new TableColumn(table, SWT.NONE);
						column.setText(SUGGEST_WITDH_FONT11);
						column.setWidth(80);
						column.setToolTipText(String.valueOf(80));
						column = new TableColumn(table, SWT.NONE);
						column.setText(SUGGEST_WITDH_FONT10);
						column.setWidth(80);
						column.setToolTipText(String.valueOf(80));
					}
				}
				column = new TableColumn(table, SWT.NONE);
				column.setText(Const.ITEM_RULES);
				column.setWidth(Const.DEFAULT_WIDTH * 2);
				column.setToolTipText(String.valueOf(Const.DEFAULT_WIDTH * 2));
				EditorCreator.createItemRulesComboCell(table, itemRules);
				column = new TableColumn(table, SWT.NONE);
				column.setText(Const.ITEM_RULES_LABEL);
				column.setWidth(Const.DEFAULT_WIDTH * 2);
				column.setToolTipText(String.valueOf(Const.DEFAULT_WIDTH * 2));
				EditorCreator.createTextEditor(table, itemRules,
						Const.ITEM_RULES_LABEL);
				item.setControl(table);
			}
			if (item.getText().equals(ComponentClass.window)) {
				for (int j = 0; j < PropertyClass.propertiesForWindow.length; j++) {
					column = new TableColumn(table, SWT.NONE);
					column.setText(PropertyClass.propertiesForWindow[j]);
					column.setWidth(PropertyClass.proColForWindow[j][0]);
					column.setToolTipText(String
							.valueOf(PropertyClass.proColForWindow[j][0]));
					column.setAlignment(PropertyClass.proColForWindow[j][1]);
				}
				item.setControl(table);
			}
		}
	}

	private void setData(Document document) {
		NodeList nodeList = document
				.getElementsByTagName(ComponentClass.tablecelleditor);
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			NamedNodeMap nodeMap = node.getAttributes();
			String[][] strings = new String[3][];
			strings[0] = nodeMap.getNamedItem(PropertyClass.colwidth)
					.getNodeValue().split(";");
			strings[1] = nodeMap.getNamedItem(PropertyClass.header)
					.getNodeValue().split(";");
			strings[2] = nodeMap.getNamedItem(PropertyClass.alignment)
					.getNodeValue().split(";");
			mapQueue.put(nodeMap.getNamedItem(PropertyClass.id).getNodeValue(),
					strings);
		}

		for (int n = 1; n < tabFolder.getItemCount(); n++) {
			Table table = (Table) mapTable.get(tabFolder.getItem(n).getText());
			if (table != null) {
				table.removeAll();
			}
		}
		NodeList nodeListField = document.getChildNodes().item(0)
				.getChildNodes();
		Table table;
		listNode.clear();
		listItem.clear();
		for (int i = 1; i < nodeListField.getLength(); i += 2) {
			Node node = nodeListField.item(i);
			NodeModel nodeModel = new NodeModel(node);
			idItem.add(nodeModel.getId());
			if (comboKeyFilter.getText().length() > 0) {
				if (nodeModel.getId().indexOf(
						comboKeyFilter.getText().toUpperCase()) < 0
						&& !nodeModel.getName().equals(ComponentClass.window)) {
					continue;
				}
			}
			listNode.add(node);
			if (node.getNodeName().equals(ComponentClass.text)) {
				ItemRules rules = new ItemRules(node);
				System.out.print(rules.toString() + "\n");
			}
			// System.out.print(node.getNodeName() + "\n");
			table = (Table) mapTable.get(node.getNodeName());
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, String.valueOf(table.getItemCount()));
			listItem.add(item);
			NamedNodeMap nodeMap = node.getAttributes();
			for (int k = 1; k < table.getColumnCount(); k++) {
				String proper = table.getColumn(k).getText();
				Node node2 = nodeMap.getNamedItem(proper);
				if (node2 != null) {
					item.setText(k, XMLUtil.getData(node2.getNodeValue(), ""));
				}
				Node node3 = nodeMap.getNamedItem(PropertyClass.put);
				if (node3 == null) {
					continue;
				}
				String listName = node3.getNodeValue();
				if (listName.indexOf(Const.LIST) >= 0) {
					String[][] string = (String[][]) mapQueue.get(listName);
					node3 = nodeMap.getNamedItem(PropertyClass.colno);
					if (node3 == null) {
						continue;
					}
					int colno = Integer.valueOf(node3.getNodeValue())
							.intValue();
					if (proper.equals(PropertyClass.width)
							&& colno < string[0].length) {
						item.setText(k, string[0][colno]);
					}
					if (proper.equals(PropertyClass.header)
							&& colno < string[1].length) {
						item.setText(k, string[1][colno]);
					}
					if (proper.equals(PropertyClass.alignment)
							&& colno < string[2].length) {
						item.setText(k, string[2][colno]);
					}
				}
			}
			Validator.validateSDLByNode((Table) mapTable
					.get(ComponentClass.LOG), item, node, menuItemListByNode);
			Validator.validateSDLByInputFile((Table) mapTable
					.get(ComponentClass.LOG), item, node, itemRules,
					menuItemListByNode);
		}
		Validator.validateSDL((Table) mapTable.get(ComponentClass.LOG),
				listItem, listNode, menuItemList);
		String tmp = comboKeyFilter.getText();
		comboKeyFilter.removeAll();
		Iterator ite = idItem.iterator();
		while (ite.hasNext()) {
			comboKeyFilter.add((String) ite.next());
		}
		comboKeyFilter.setText(tmp);
	}

	private void hideBlankData(boolean isHide) {
		for (int n = tabFolder.getItemCount() - 2; n > 0; n--) {
			Table table = (Table) mapTable.get(tabFolder.getItem(n).getText());
			for (int i = 1; i < table.getColumnCount(); i++) {
				TableColumn column = table.getColumn(i);
				int check = 0;

				for (int j = 0; j < table.getItemCount(); j++) {
					if (table.getItem(j).getText(i).equals("")) {
						check++;
					}
					if (column.getText().equals(Const.SHOW_LENGTH)
							|| column.getText().equals(Const.ITEM_RULES)
							|| column.getText().equals(Const.ITEM_RULES_LABEL)) {
						table.getItem(j).setBackground(i, Const.WHITE);
					}
				}
				if (column.getText().equals(Const.SHOW_LENGTH)
						|| column.getText().equals(SUGGEST_WITDH_FONT10)
						|| column.getText().equals(SUGGEST_WITDH_FONT11)
						|| column.getText().equals(Const.ITEM_RULES)
						|| column.getText().equals(Const.ITEM_RULES_LABEL)) {
					continue;
				}
				if (check == table.getItemCount() && isHide) {
					column.setWidth(0);
					column.setResizable(false);
				} else {
					column.setWidth(Integer.parseInt(XMLUtil.getData(column
							.getToolTipText(), String
							.valueOf(Const.DEFAULT_WIDTH))));
					column.setResizable(true);
				}
			}
			if (table.getItemCount() == 0
					|| tabFolder.getItem(n).getControl() == null) {
				tabFolder.getItem(n).dispose();
			}
		}
	}

	private void openFile() {
		String filepath = new String();
		try {
			RandomAccessFile raf = new RandomAccessFile(new File(FILE_PATH),
					"r");
			filepath = raf.readLine();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		FileDialog fileXmlDialog = new FileDialog(shell);
		fileXmlDialog.setText("Search SDL Files");
		fileXmlDialog.setFilterExtensions(new String[] { "*.sdl" });
		System.out.print(filepath);
		fileXmlDialog.setFilterPath(filepath);
		String strResult = fileXmlDialog.open();
		if (strResult != null) {
			filename = strResult;
			txtFilename.setText(filename);
		}
	}

	private void validate() {
		Document document = XMLUtil.readXML(txtFilename.getText());
		if (document == null) {
			return;
		}
		createTabItem();
		setData(document);
		hideBlankData(true);
		hideCellWidthValidator(true);
		chkCellWidth.setEnabled(true);
	}

	private void hideCellWidthValidator(boolean isHide) {
		String[] temp = new String[] { ComponentClass.text,
				ComponentClass.label };
		for (int n = 0; n < temp.length; n++) {
			Table table = (Table) mapTable.get(temp[n]);
			for (int i = 1; i < table.getColumnCount(); i++) {
				TableColumn column = table.getColumn(i);
				if (column.getText().equals(Const.SHOW_LENGTH)
						|| column.getText().equals(SUGGEST_WITDH_FONT10)
						|| column.getText().equals(SUGGEST_WITDH_FONT11)) {
					if (isHide) {
						column.setWidth(0);
						column.setResizable(false);
					} else {
						column.setWidth(Integer.parseInt(XMLUtil.getData(column
								.getToolTipText(), String
								.valueOf(Const.DEFAULT_WIDTH))));
						column.setResizable(true);
					}
				}
			}
		}
	}

	private void createLogTab() {
		TabItem item = new TabItem(tabFolder, SWT.NONE, 0);
		item.setText(ComponentClass.LOG);
		final Table table = new Table(tabFolder, SWT.FULL_SELECTION
				| SWT.VIRTUAL | SWT.MULTI | SWT.CASCADE);
		table.addMouseListener(new MouseListener() {
			public void mouseUp(MouseEvent arg0) {
			}

			public void mouseDown(MouseEvent arg0) {
			}

			public void mouseDoubleClick(MouseEvent arg0) {
				TableItem item = (TableItem) table.getSelection()[0]
						.getData(Const.TABLEITEM);
				for (int i = 0; i < tabFolder.getItemCount(); i++) {
					if (tabFolder.getItem(i).getControl().equals(
							item.getParent())) {
						tabFolder.setSelection(i);
						item.getParent().forceFocus();
						item.getParent().setSelection(item);
					}
				}
			}
		});
		table.addListener(SWT.DefaultSelection, new Listener() {
			public void handleEvent(Event arg0) {
				table.forceFocus();
				table.deselectAll();
			}
		});
		item.setControl(table);
		table.setBackground(Const.DEFAULT_COLOR);
		mapTable.put(ComponentClass.LOG, table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableColumn column = new TableColumn(table, SWT.NONE);
		column.setText("No.");
		column.setWidth(40);
		column.setAlignment(SWT.CENTER);
		column = new TableColumn(table, SWT.NONE);
		column.setText("Type");
		column.setWidth(150);
		column = new TableColumn(table, SWT.NONE);
		column.setText("Detail");
		column.setWidth(200);
		column = new TableColumn(table, SWT.NONE);
		column.setText("ID");
		column.setWidth(150);
		column = new TableColumn(table, SWT.NONE);
		column.setText("Note");
		column.setWidth(300);
		column = new TableColumn(table, SWT.NONE);
		column.setText("Comment");
		column.setWidth(0);

		TabItem item2 = new TabItem(tabFolder, SWT.NONE, 1);
		item2.setText(Const.ITEM_RULES_LABEL);
		OleFrame frame;
		try {
			frame = new OleFrame(tabFolder, SWT.NONE);
			frame.setWindowMenus(shell.getMenuBar().getItems());
			OleClientSite site;
			site = new OleClientSite(frame, SWT.NONE, new File(FILE_ITEM_RULES));
			site.forceFocus();
		} catch (SWTError e) {
			System.out.println("Unable to open activeX control");
			return;
		}
		item2.setControl(frame);
	}

	private void saveCSVFile() {
		if ((Table) mapTable.get(ComponentClass.window) == null) {
			return;
		}
		FileDialog fileXmlDialog2 = new FileDialog(shell, SWT.SAVE);
		fileXmlDialog2.setText("Save CSV result file");
		fileXmlDialog2.setFilterExtensions(new String[] { "*.csv" });
		fileXmlDialog2
				.setFileName(((Table) mapTable.get(ComponentClass.window))
						.getItem(0).getText(1)
						+ "_CheckResult");
		String strResult = fileXmlDialog2.open();
		if (strResult != null) {
			try {
				RandomAccessFile raf = new RandomAccessFile(
						new File(strResult), "rw");
				Table table = (Table) mapTable.get(ComponentClass.LOG);
				for (int i = 0; i < table.getColumnCount() - 1; i++) {
					raf.writeBytes(table.getColumn(i).getText() + ",");
				}
				raf.writeBytes(table.getColumn(table.getColumnCount() - 1)
						.getText()
						+ "\r\n");
				for (int i = 0; i < table.getItemCount(); i++) {
					for (int j = 0; j < table.getColumnCount() - 1; j++) {
						raf.writeBytes(table.getItem(i).getText(j) + ",");
					}
					raf.writeBytes(table.getItem(i).getText(
							table.getColumnCount() - 1)
							+ "\r\n");
				}
				raf.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
